#include "HY1C_out.h"
#include <strings.h>
#include "l1a.h"
#include "l1_octs_hdf.h"
#include "hdf_utils.h"
#include "l12_proto.h"

#define   LAC_PIXEL_NUM           1285
#define   NREC_IN_BUF             10
#define   STBUFSIZ                5


static unsigned char genBuf[8192];

int32 seawifs_meta(filehandle *l1file, filehandle *outfile, 
		   int32 sscan, int32 escan, int32 dscan,
                   int32 spix, int32 dpix);


int32 l2_seawifs(filehandle *l1file, filehandle *l2file)
{
  int32 status;

  status = seawifs_meta(l1file, l2file, 
			l1file->input->sline - 1, l1file->input->eline -1, 
			l1file->input->dline, l1file->input->spixl - 1,
			l1file->input->dpixl);

  return 0;
}




int32 l1b_seawifs(filehandle *l1file, filehandle *ofile, 
		  int32 sscan, int32 escan, int32 dline, 
                  int32 spixl, int32 epixl, int32 dpixl)
{
  int32 numBands = l1file->nbands;
  int32 i;
  int32 k;
  int32 status;
  int32 n_read;
  int32 max_rec_in_rdbuf;
  int32 start[3] = {0,0,0};
  int32 edges[3];
  int32 stride[3] = {1,1,1};
  int32 sline = sscan+1;  
  int32 eline = escan+1;

  int32 nscan = (eline-sline)/dline + 1;

  int32 write_scan = 1;

  char *fld_name[] = {"s_satp","s_zerop","sc_id","sc_ttag","sc_soh",
		      "inst_tlm","start_syn","stop_syn","dark_rest",
		      "sc_ana","sc_dis","END"};
  int32 fld_2dim[] = {8,8,2,4,775,44,8,8,8,40,40};


  status = seawifs_meta(l1file, ofile,
			sscan, escan, dline, spixl, dpixl);

  int32 l1a_sd_id = l1file->sd_id;
  int32 l1b_sd_id = ofile->sd_id;

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "s_satp",                                        /* short name      */
    "Number of saturated pixels per band",           /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_INT16,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan,numBands,1,                                /* dimension sizes */
    "Number of Scan Lines", "band number", NULL      /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "s_zerop",                                       /* short name      */
    "Number of zero pixels per band",                /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_INT16,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan,numBands,1,                                /* dimension sizes */
    "Number of Scan Lines", "band number", NULL      /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "sc_id",                                         /* short name      */
    "Spacecraft ID",                                 /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_INT16,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan, 2, 1,                                     /* dimension sizes */
    "Number of Scan Lines", "2", NULL                /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "sc_ttag",                                       /* short name      */
    "Spacecraft time tag",                           /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_INT16,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan, 4, 1,                                     /* dimension sizes */
    "Number of Scan Lines", "4", NULL                /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "sc_soh",                                        /* short name      */
    "Spacecraft state-of-health",                    /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_UINT8,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan, 775, 1,                                   /* dimension sizes */
    "Number of Scan Lines", "775", NULL              /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "inst_tlm",                                      /* short name      */
    "SeaWiFS instrument telemetry",                  /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_INT16,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan, 44, 1,                                    /* dimension sizes */
    "Number of Scan Lines", "44", NULL               /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "start_syn",                                     /* short name      */
    "Start-synch pixel",                             /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_INT16,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan,numBands,1,                                /* dimension sizes */
    "Number of Scan Lines", "band number", NULL      /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "stop_syn",                                      /* short name      */
    "Stop-synch pixel",                              /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_INT16,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan,numBands,1,                                /* dimension sizes */
    "Number of Scan Lines", "band number", NULL      /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "dark_rest",                                     /* short name      */
    "Dark-restore pixel",                            /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_INT16,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan,numBands,1,                                /* dimension sizes */
    "Number of Scan Lines", "band number", NULL      /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "sc_ana",                                        /* short name      */
    "Spacecraft analog telemetry",                   /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_FLOAT32,                                    /* HDF number type */
    2,                                               /* rank            */
    nscan, 40, 1,                                    /* dimension sizes */
    "Number of Scan Lines", "40", NULL               /* dimension names */
    ) );

    PTB( CreateSDS(
    l1b_sd_id,                                       /* file id         */
    "sc_dis",                                        /* short name      */
    "Spacecraft discrete telemetry",                 /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    DFNT_UINT8,                                      /* HDF number type */
    2,                                               /* rank            */
    nscan, 40, 1,                                    /* dimension sizes */
    "Number of Scan Lines", "40", NULL               /* dimension names */
    ) );


    /* Copy SDS data */
    max_rec_in_rdbuf = sline-1;

    for (i=sline; i<=eline; i++) {

      if (i >  max_rec_in_rdbuf) {

	n_read = (eline - max_rec_in_rdbuf) / dline;
	if ((eline - max_rec_in_rdbuf) % dline != 0) n_read++;
	n_read = (n_read >= NREC_IN_BUF) ? NREC_IN_BUF : n_read;

	k = 0;
	while (strcmp(fld_name[k],"END") != 0) {

	  start[0] = i-1;
	  stride[0] = dline;
	  edges[0] = n_read;
	  edges[1] = fld_2dim[k];
	  status = SDreaddata(SDselect(l1a_sd_id, 
				       SDnametoindex(l1a_sd_id,fld_name[k])), 
			      start, stride, edges, (VOIDP) genBuf);

	  start[0] = write_scan - 1;
	  stride[0] = 1;
	  status = SDwritedata(SDselect(l1b_sd_id, 
				       SDnametoindex(l1b_sd_id,fld_name[k])), 
			      start, stride, edges, (VOIDP) genBuf);
	  k++;
	}

	max_rec_in_rdbuf += n_read * dline;
	write_scan += n_read;
      }
      
    }



  return 0;
}



int32 seawifs_meta(filehandle *l1file, filehandle *ofile, 
		   int32 sscan, int32 escan, int32 dscan, 
		   int32 spix, int32 dpix)
{
  int32 numBands = l1file->nbands;
  int32 i;
  int32 k;
  int32 sd_id;
  int32 max_rec_in_rdbuf;
  int32 status;
  int32 n_read;
  int32 start[3] = {0,0,0};
  int32 edges[3];
  int32 stride[3] = {1,1,1};

  int32 sline;
  int32 eline;
  int32 dline;
  int32 spixl;
  int32 dpixl;
  int32 nscan;

  int32 l1a_spixl;
  int32 l1a_dpixl;

  int32 write_scan = 1;

  int32 ntilts;
  int16 tilt_flags[20];
  int16 tilt_ranges[2*20];
  int16 *tf;

  char *fld_name[] = {"orb_vec","sun_ref","att_ang","scan_ell","nflag",
		      "sen_mat","END"};
  int32 fld_2dim[] = {3,3,3,6,8,3};

  extern float64         t_const[BANDS_DIMS_1A];
  extern float64         t_linear_1[BANDS_DIMS_1A];
  extern float64         t_exponential_1[BANDS_DIMS_1A];
  extern float64         t_linear_2[BANDS_DIMS_1A];
  extern float64         t_exponential_2[BANDS_DIMS_1A];
  extern float64         cal_offs[BANDS_DIMS_1A];
  extern float64         ms1_const[BANDS_DIMS_1A];
  extern float64         ms1_linear_1[BANDS_DIMS_1A];
  extern float64         ms1_exponential_1[BANDS_DIMS_1A];
  extern float64         ms1_linear_2[BANDS_DIMS_1A];
  extern float64         ms1_exponential_2[BANDS_DIMS_1A];
  extern float64         ms2_const[BANDS_DIMS_1A];
  extern float64         ms2_linear_1[BANDS_DIMS_1A];
  extern float64         ms2_exponential_1[BANDS_DIMS_1A];
  extern float64         ms2_linear_2[BANDS_DIMS_1A];
  extern float64         ms2_exponential_2[BANDS_DIMS_1A];

  extern int16           entry_year;
  extern int16           entry_day;
  extern int16           ref_year;
  extern int16           ref_day;
  extern int16           ref_minute;
  extern float32         counts[BANDS_DIMS_1A][GAINS_DIMS_1A][KNEES_DIMS_1A];
  extern float32         rads[BANDS_DIMS_1A][GAINS_DIMS_1A][KNEES_DIMS_1A];

  int32 l1a_sd_id = l1file->sd_id;

  sd_id = ofile->sd_id;
  nscan = (escan - sscan)/dscan + 1;

  sline = sscan + 1;
  eline = escan + 1;
  dline = dscan;
  
  idDS ds_id;
  ds_id.fid = sd_id;
  ds_id.fftype = ofile->format;

  if ( ofile->format == FMT_L2NCDF) {
      int dumdim;
    if ( nc_def_dim(ds_id.fid, "vec", 3, &dumdim)
	 != NC_NOERR) exit(1);
    if ( nc_def_dim(ds_id.fid, "ell", 6, &dumdim)
	 != NC_NOERR) exit(1);
    if ( nc_def_dim(ds_id.fid, "nflag", 8, &dumdim)
	 != NC_NOERR) exit(1);
    if ( nc_def_dim(ds_id.fid, "mtilt", 20, &dumdim)
	 != NC_NOERR) exit(1);
    if ( nc_def_dim(ds_id.fid, "ltilt", 2, &dumdim)
	 != NC_NOERR) exit(1);
    if ( nc_def_dim(ds_id.fid, "unit", 1, &dumdim)
	 != NC_NOERR) exit(1);
  }
 
  int32 nt_chr, nt_i16, nt_i32, nt_f32;
  if ( ofile->format == FMT_L1HDF || ofile->format == FMT_L2HDF) {
    nt_chr = DFNT_CHAR;
    nt_i16 = DFNT_INT16;
    nt_i32 = DFNT_INT32;
    nt_f32 = DFNT_FLOAT32;
  } else if ( ofile->format == FMT_L2NCDF) {
    nt_chr = NC_CHAR;
    nt_i16 = NC_SHORT;
    nt_i32 = NC_INT;
    nt_f32 = NC_FLOAT;
  }

  if ( ofile->format == FMT_L2NCDF) ds_id.fid = ofile->grp_id[4];

    PTB( createDS(
    ds_id,                                           /* file id         */
    "orb_vec",                                       /* short name      */
    "Orbit position vector at scan line time",       /* long name       */
    NULL,                                            /* standard name   */
    "kilometers",                                    /* units           */
    -7200, 7200,                                     /* valid range     */
    0,0,                                             /* slope           */
    nt_f32,                                          /* HDF number type */
    2,                                               /* rank            */
    nscan, 3, 1,                                     /* dimension sizes */
    "Number of Scan Lines", "vec", NULL              /* dimension names */
    ) );

    PTB( createDS(
    ds_id,                                           /* file id         */
    "sun_ref",                                       /* short name      */
    "Reference Sun vector in ECEF frame",            /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    -1, 1,                                           /* valid range     */
    0,0,                                             /* slope           */
    nt_f32,                                          /* HDF number type */
    2,                                               /* rank            */
    nscan, 3, 1,                                     /* dimension sizes */
    "Number of Scan Lines", "vec", NULL              /* dimension names */
    ) );

    PTB( createDS(
    ds_id,                                           /* file id         */
    "att_ang",                                       /* short name      */
    "Computed yaw, roll, pitch",                     /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    -180, 180,                                       /* valid range     */
    0,0,                                             /* slope           */
    nt_f32,                                          /* HDF number type */
    2,                                               /* rank            */
    nscan, 3, 1,                                     /* dimension sizes */
    "Number of Scan Lines", "vec", NULL              /* dimension names */
    ) );

    PTB( createDS(
    ds_id,                                           /* file id         */
    "sen_mat",                                       /* short name      */
    "ECEF-to-sensor-frame matrix",                   /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    -1, 1,                                           /* valid range     */
    0,0,                                             /* slope           */
    nt_f32,                                          /* HDF number type */
    3,                                               /* rank            */
    nscan, 3, 3,                                     /* dimension sizes */
    "Number of Scan Lines", "vec", "vec"             /* dimension names */
    ) );

    PTB( createDS(
    ds_id,                                           /* file id         */
    "scan_ell",                                      /* short name      */
    "Scan-track ellipse coefficients",               /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    nt_f32,                                          /* HDF number type */
    2,                                               /* rank            */
    nscan, 6, 1,                                     /* dimension sizes */
    "Number of Scan Lines", "ell", NULL              /* dimension names */
    ) );

    PTB( createDS(
    ds_id,                                           /* file id         */
    "nflag",                                         /* short name      */
    "Navigation flags",                              /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    nt_i32,                                          /* HDF number type */
    2,                                               /* rank            */
    nscan, 8, 1,                                     /* dimension sizes */
    "Number of Scan Lines", "nflag", NULL            /* dimension names */
    ) );

    if ( ofile->format == FMT_L2NCDF) ds_id.fid = ofile->grp_id[1];

    PTB( createDS(
    ds_id,                                           /* file id         */
    "ntilts",                                        /* short name      */
    "Number of scene tilt states",                   /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    nt_i32,                                          /* HDF number type */
    1,                                               /* rank            */
    1, 1, 1,                                         /* dimension sizes */
    "unit", NULL, NULL                               /* dimension names */
    ) );

    PTB( createDS(
    ds_id,                                           /* file id         */
    "tilt_flags",                                    /* short name      */
    "Tilt indicators",                               /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    -1, 3,                                           /* valid range     */
    0,0,                                             /* slope           */
    nt_i16,                                          /* HDF number type */
    1,                                               /* rank            */
    20, 1, 1,                                        /* dimension sizes */
    "mtilt", NULL, NULL                              /* dimension names */
    ) );

    PTB( createDS(
    ds_id,                                           /* file id         */
    "tilt_ranges",                                   /* short name      */
    "Scan-line number ranges of scene tilt states",  /* long name       */
    NULL,                                            /* standard name   */
    NULL,                                            /* units           */
    0, 0,                                            /* valid range     */
    0,0,                                             /* slope           */
    nt_i16,                                          /* HDF number type */
    2,                                               /* rank            */
    20, 2, 1,                                        /* dimension sizes */
    "mtilt", "ltilt", NULL                           /* dimension names */
    ) );



    /* Copy SDS data in "fld_name" array */
    /* --------------------------------- */
    max_rec_in_rdbuf = sline - 1;

    if ( ofile->format == FMT_L1HDF || ofile->format == FMT_L2HDF) {
      ds_id.fid = ofile->sd_id;
    } else if ( ofile->format == FMT_L2NCDF) {
      ds_id.fid = ofile->grp_id[4];
    }

    for (i=sline; i<=eline; i++) {

      if (i > max_rec_in_rdbuf) {

	n_read = (eline - max_rec_in_rdbuf) / dline;
	if ((eline - max_rec_in_rdbuf) % dline != 0) n_read++;
	n_read = (n_read >= NREC_IN_BUF) ? NREC_IN_BUF : n_read;

	k = 0;
	while (strcmp(fld_name[k],"END") != 0) {
	  if (strcmp(fld_name[k],"sen_mat") == 0) edges[2] = 3;

	  start[0] = i-1;
	  stride[0] = dline;
	  edges[0] = n_read;
	  edges[1] = fld_2dim[k];
	  status = SDreaddata(SDselect(l1a_sd_id, 
				       SDnametoindex(l1a_sd_id,fld_name[k])), 
			      start, stride, edges, (VOIDP) genBuf);

	  start[0] = write_scan - 1;
	  stride[0] = 1;

	  PTB( writeDS(ds_id, fld_name[k], genBuf, 
		       start[0],start[1],start[2],edges[0],edges[1],edges[2]) );

	  /*
	  status = SDwritedata(SDselect(sd_id, 
				       SDnametoindex(sd_id,fld_name[k])), 
			      start, stride, edges, (VOIDP) genBuf);
	  */
	  k++;
	}

	max_rec_in_rdbuf += n_read * dline;
	write_scan += n_read;
      }
      
    }


    /* Write Nav Flag SDS */

    /* Write Tilt SDS */
    /* -------------- */
    status = rdSDS(l1a_sd_id,"ntilts",0,0,0,0,(VOIDP) &ntilts); 
    status = rdSDS(l1a_sd_id,"tilt_flags",0,0,0,0,(VOIDP) tilt_flags); 
    status = rdSDS(l1a_sd_id,"tilt_ranges",0,0,0,0,(VOIDP) tilt_ranges); 


    /* Handle subsetted and/or subsampled case */
    /* --------------------------------------- */
    if (sline != 1 || eline != l1file->nscan || dline != 1) {

      /* Build tilt flag (tf) array */
      tf = (short int *) calloc(l1file->nscan, sizeof(short int));
      for (k=0; k<ntilts; k++) 
	for (i=tilt_ranges[k*2]-1; i<tilt_ranges[k*2+1]; i++) 
	  tf[i] = tilt_flags[k]; 

      /* Loop through tf array and build tilt_flags & tilt_ranges */
      k = 0;
      ntilts = 1;
      tilt_flags[0] = tf[sline-1];
      for (i=sline-1; i<eline; i+=dline) {
	k++;
	if (tf[i] != tilt_flags[ntilts-1]) {
	  tilt_flags[ntilts] = tf[i];

	  tilt_ranges[2*ntilts-1] = k-1;
	  tilt_ranges[2*ntilts] = k;

	  ntilts++;
	}
      }
      tilt_ranges[2*ntilts-1] = k;
      for (i=2*ntilts; i<40; i++) tilt_ranges[i] = 0;
      for (i=ntilts; i<20; i++) tilt_flags[i] = 0;

      free(tf);
    }

    if ( ofile->format == FMT_L1HDF || ofile->format == FMT_L2HDF) {
      ds_id.fid = ofile->sd_id;
    } else if ( ofile->format == FMT_L2NCDF) {
      ds_id.fid = ofile->grp_id[1];
    }

    PTB( writeDS(ds_id, "ntilts", &ntilts, 0, 0, 0, 1, 0, 0) );
    PTB( writeDS(ds_id, "tilt_flags", tilt_flags, 0, 0, 0, 20, 0, 0) );
    PTB( writeDS(ds_id, "tilt_ranges", tilt_ranges, 0, 0, 0, 20, 2, 0) );


    /* Write Global Attributes */
    ds_id.fid = ofile->sd_id;
    if ( ofile->format == FMT_L1HDF || ofile->format == FMT_L2HDF) {
      ds_id.sid = ds_id.fid;
    } else if ( ofile->format == FMT_L2NCDF) {
      ds_id.sid = NC_GLOBAL;
    }

    status = getHDFattr(l1a_sd_id, "Orbit Number", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Orbit Number", nt_i32, 1, genBuf) );

    bzero(genBuf, sizeof(genBuf));
    status = getHDFattr(l1a_sd_id, "Data Center", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Data Center", nt_chr, strlen((const char *) genBuf)+1, (VOIDP) genBuf) );

    bzero(genBuf, sizeof(genBuf));
    status = getHDFattr(l1a_sd_id, "Station Name", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Station Name", nt_chr, strlen((const char *) genBuf)+1, (VOIDP)  genBuf) );

    status = getHDFattr(l1a_sd_id, "Station Latitude", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Station Latitude", nt_f32, 1, (VOIDP) genBuf) );

    status = getHDFattr(l1a_sd_id, "Station Longitude", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Station Longitude", nt_f32, 1, (VOIDP) genBuf) );

    bzero(genBuf, sizeof(genBuf));
    status = getHDFattr(l1a_sd_id, "Mission", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Mission", nt_chr, strlen((const char *) genBuf)+1, (VOIDP) genBuf) );

    bzero(genBuf, sizeof(genBuf));
    status = getHDFattr(l1a_sd_id, "Mission Characteristics", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Mission Characteristics", nt_chr, strlen((const char *) genBuf)+1, (VOIDP) genBuf) );

    bzero(genBuf, sizeof(genBuf));
    status = getHDFattr(l1a_sd_id, "Sensor", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Sensor", nt_chr, strlen((const char *) genBuf)+1, (VOIDP) genBuf) );

    status = getHDFattr(l1a_sd_id, "Orbit Node Longitude", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Orbit Node Longitude", nt_f32, 1, (VOIDP) genBuf) );

    bzero(genBuf, sizeof(genBuf));
    status = getHDFattr(l1a_sd_id, "Sensor Characteristics", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Sensor Characteristics", nt_chr, strlen((const char *) genBuf)+1, (VOIDP) genBuf) );

    bzero(genBuf, sizeof(genBuf));
    status = getHDFattr(l1a_sd_id, "Data Type", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Data Type", nt_chr, strlen((const char *) genBuf)+1, (VOIDP) genBuf) );

    status = getHDFattr(l1a_sd_id, "LAC Pixel Start Number", "", (VOIDP) &l1a_spixl);
    status = getHDFattr(l1a_sd_id, "LAC Pixel Subsampling", "",  (VOIDP) &l1a_dpixl);

    spixl = (l1a_dpixl * spix) + l1a_spixl;
    dpixl = l1a_dpixl * dpix;

    PTB(setAttr( ds_id, "LAC Pixel Start Number", nt_i32, 1, (VOIDP) &spixl) );
    PTB(setAttr( ds_id, "LAC Pixel Subsampling", nt_i32, 1,  (VOIDP) &dpixl) );

    bzero(genBuf, sizeof(genBuf));
    status = getHDFattr(l1a_sd_id, "Node Crossing Time", "", (VOIDP) &genBuf);
    PTB(setAttr( ds_id, "Node Crossing Time", nt_chr, strlen((const char *) genBuf)+1, (VOIDP) genBuf) );

  return 0;
}



